//
//  ViewController.m
//  Test
//
//  Created by xgc on 12/15/17.
//  Copyright © 2017 xgc. All rights reserved.
//

#import "ViewController.h"
#import "demo.h"
#import "oc/CoinApi.h"
#import "oc/CANetParams.h"
#import "oc/CAKeyPath.h"
#import "oc/CABtWallet.h"
#import "oc/BtcTransactionSignParams.h"
#import "oc/CABtcVout.h"
#import "oc/EthTransactionSignParams.h"


@interface ViewController ()

@end

@implementation ViewController
{
    CommonCoinApi* cca;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    test();
    
    test2();

    /*
    cca = new CommonCoinApi();
    
    if(cca) {
        vector<string> words;

        words = cca->createAllCoinMnemonicCode();
        
        for (const auto& word: words)
        {
            bc::cout << word << std::endl;
        }
        
        bool ret = cca->checkMnemonicCode(words);
        bc::cout << ret << std::endl;
        
        string password = "12345";
        NetParams netParams;
        netParams.setSymbol("ETH");
        netParams.setCoinType(CoinType::ETH);
        netParams.setNetType(NetworkType::TEST);
        netParams.setVersion(0x00000002);
        KeyPath keyPath;
        keyPath.setSymbol("ETH");
        keyPath.setPath1(0);
        keyPath.setPath2(1);
        keyPath.setPath3(0);
        
        netParams.setKeyPath(keyPath);
        
        string strWords;
        
        int size = words.size();
        for(int i = 0; i < size; i++) {
            strWords += words[i];
            if(i != size - 1)
                strWords += " ";
        }
        
        BtWallet bw;
        
        ret = cca->createWallet(strWords, password, netParams, bw);
        bc::cout << ret << std::endl;
        
        ret = cca->validateAddress(bw.getAddress(), netParams);
        bc::cout << ret << std::endl;
        
        string strPriv = cca->getPriKeyFromBtSeed(*bw.getBtSeed(), password, netParams);
        bc::cout << strPriv << std::endl;
        
        vector<string> wl;
        wl = cca->getMnemonicCodeFromBtWallet(*bw.getBtSeed(), password, netParams);
        
        size = wl.size();
        for(int i = 0; i < size; i++) {
            bc::cout << wl[i] << std::endl;
        }
        
        string newPassword = "abcde";
        BtSeed newBtSeed;
        ret = cca->changePassword(*bw.getBtSeed(), password, newPassword, newBtSeed);
        
        bc::cout << ret << std::endl;

        BtcCreateSignTransactionParams params;
        params.setBtSeed(newBtSeed);
        params.setPriKey("cQudEARWPP3f3uV192QaR9k5zNb5myfPnu72Fj1KqkNTfjB46Hn9");
        params.setPassword(newPassword);
        params.setFeePerKb(10000);
        params.setFromAddress("mra3Ek41AeHvupSVDP1hPqkkfm3MSrUpyy");
        params.setSendAddress("n4qExpxGtSR9neL1gKJGXCgxto8TyD5YwW");
        params.setSendValue(100000000);
        
        vector<BtcvoutForm> voutList;
        BtcvoutForm vout1;
        vout1.setHash("e06a7cd2b500afa588b5035f506fff88ed303d3e64468fcf0789fe646701ecf8");
        vout1.setValue(5000000000);
        vout1.setN(0);
        vout1.setCoinBase(true);
        voutList.push_back(vout1);
        params.setBtcvoutFormList(voutList);
  
        EthCreateSignTransactionParams jethSignParams;
        jethSignParams.setPassword(newPassword);
        
        jethSignParams.setFrom("0x6920706b07b94980ac2391ea8935b67c8b7f2d1b");
        jethSignParams.setTo("0xaca35562d3555e61120a151b3c8e8e91d83a312b");

        jethSignParams.setGasLimit(100000);

        jethSignParams.setGasPrice(10000);

        jethSignParams.setValue(10000);
        jethSignParams.setData("");

        jethSignParams.setNonce(0);
        jethSignParams.setPriKey("cfa37380330a54fa3b7d74aba4460da4c87016f4984b67af46257ea1ec8b10dc");
        
        map<string, string> resultMap;
        
        ret = cca->createSignTransaction(&jethSignParams, netParams, resultMap);
        
        bc::cout << ret << std::endl;
  
    }
    */
    
    
    CoinApi* coinApi = [CoinApi sharedManager];
    
    NSMutableArray* words = [coinApi createAllCoinMnemonicCode];
    NSLog(@"%@", words);
    
    BOOL ret = [coinApi checkMnemonicCode:words];
    NSLog(@"%d", ret);
    
    NSString* password = @"12345";
    CANetParams* netParams = [[CANetParams alloc]init];
    netParams.csymbol = @"LBTC";
    netParams.coinType = CoinType::BTC;
    netParams.netType = NetworkType::MAIN;
    netParams.version = 0xff01;
    
    CAKeyPath* keyPath = [[CAKeyPath alloc] init];
    keyPath.symbol = @"LBTC";
    keyPath.path1 = 0;
    keyPath.hd1 = YES;
    keyPath.path2 = 1;
    keyPath.hd2 = YES;
    keyPath.path3 = 0;
    keyPath.hd3 = NO;
    
    netParams.keyPath = keyPath;
    
    netParams.ApiVersion =2;
    
    netParams.HDprivate = 0x04358394;
    netParams.HDpublic = 0x043587CF;
    netParams.P2KH = 0x6f;
    netParams.P2SH = 0xC4;
    netParams.Keyprefixes = 239;
    
    netParams.N = 32768;
    netParams.R = 8;
    netParams.P = 1;
    
    int size = [words count];
    int i = 0;
    NSMutableString* strWords = [[NSMutableString alloc]init];
    for(id word in words) {
        [strWords appendString:word];
        if(i != size - 1) {
            [strWords appendString:@" "];
        }
        i++;
    }
    
    CABtWallet* wallet = [coinApi createWallet:strWords withPassword:password withNetParams:netParams];
    NSLog(@"%@", wallet);
    
    ret = [coinApi validateAddress:wallet.address withNetParams:netParams];
    NSLog(@"%d", ret);
    
    NSString* strPriv = [coinApi getPriKeyFromBtSeed:wallet.btSeed withPassword:password withNetParams:netParams];
    NSLog(@"%@", strPriv);
    
    NSMutableArray* mneArray = [coinApi getMnemonicCodeFromBtSeed:wallet.btSeed withPassword:password withNetParams:netParams];
    NSLog(@"%@", mneArray);
    
    NSString* newPassword = @"abcde";
    CABtSeed* newBtSeed = [coinApi changePassword:wallet.btSeed withOldPassword:password withNewPassword:newPassword withNetParams: netParams];
    NSLog(@"%@", newBtSeed);
    
    
    BtcTransactionSignParams* btcSignParams = [[BtcTransactionSignParams alloc]init];
    btcSignParams.btSeed = wallet.btSeed;
    btcSignParams.priKey = @"KwKkAHgPrcWRRMqc6CjUr8cFPFqd1KX2VWY8Y9yCf6y6PVw7Nniu";
    btcSignParams.password = newPassword;
    btcSignParams.feePerKb = 100000;
    btcSignParams.fromAddress = @"15kdBXHVTjcwVaD41GsuhQKcgd5Rim8Pjs";
    btcSignParams.sendAddress = @"19JLS6DDU9p2MVsuGw4Em6bU3vaEdBBwdq";
    btcSignParams.sendValue = 0;
    btcSignParams.recipientsPayFees = NO;
    
    NSMutableArray* voutList = [NSMutableArray array];
    CABtcVout* vout = [[CABtcVout alloc]init];
    vout.txHash = @"2306e08bf3032efbad11123a9f8b91d376d4608cb40a639b074a173e5fe6fcff";
    vout.value = 100000000;
    vout.n = 0;
    vout.coinBase = YES;
    
    [voutList addObject:vout];
    btcSignParams.btcvoutFormList = voutList;
    
    /*
    NSMutableData* encode = [coinApi getLbtcRegister:@"1GsoHi1orC6H7TqAQjsTh7T7fMm38uYqkV" withPrikey:@"L2dSXNzTjoBGayfxTMxfhsqpWWi3d5QmjAN9d57hAunxLKpF5RSs" withName:@"123456" withNetParams:netParams];
    btcSignParams.data = encode;
    btcSignParams.txType = TxType::TX_LBTC_REGISTER;
    */
    
    NSMutableArray* addrList = [NSMutableArray array];
    [addrList addObject:@"1GsoHi1orC6H7TqAQjsTh7T7fMm38uYqkV"];
    
    /*
    NSMutableData* encode = [coinApi getLbtcVote:@"1GsoHi1orC6H7TqAQjsTh7T7fMm38uYqkV" withPrikey:@"L2dSXNzTjoBGayfxTMxfhsqpWWi3d5QmjAN9d57hAunxLKpF5RSs" withAddrList:addrList withNetParams:netParams];
    btcSignParams.data = encode;
    btcSignParams.txType = TxType::TX_LBTC_VOTE;
    */
    
    NSMutableData* encode = [coinApi getLbtcCancelVote:@"1GsoHi1orC6H7TqAQjsTh7T7fMm38uYqkV" withPrikey:@"L2dSXNzTjoBGayfxTMxfhsqpWWi3d5QmjAN9d57hAunxLKpF5RSs" withAddrList:addrList withNetParams:netParams];
    btcSignParams.data = encode;
    btcSignParams.txType = TxType::TX_LBTC_CANCELVOTE;
    
    NSMutableDictionary* resultMap = [coinApi createSignTransaction:btcSignParams withNetParams:netParams];
    NSLog(@"%@", resultMap);
    
    /*
    NSMutableData* encode = [coinApi getTokenTransferBinary:@"0xd09ff9e884e58b16cea685db5ec16cc5917d2438" withValue:@"2667000000000000000"];
    
    EthTransactionSignParams* ethSignParams = [[EthTransactionSignParams alloc] init];
    ethSignParams.btSeed = wallet.btSeed;
    ethSignParams.priKey = @"cfa37380330a54fa3b7d74aba4460da4c87016f4984b67af46257ea1ec8b10dc";
    ethSignParams.password = newPassword;
    ethSignParams.from = @"0x6920706b07b94980ac2391ea8935b67c8b7f2d1b";
    ethSignParams.to = @"0xaca35562d3555e61120a151b3c8e8e91d83a312b";
    ethSignParams.gasLimit = @"100000";
    ethSignParams.gasPrice = @"10000";
    ethSignParams.value = @"10000";
    ethSignParams.data = nil;
    ethSignParams.nonce = @"0";
    
    NSMutableDictionary* resultMap = [coinApi createSignTransaction:ethSignParams withNetParams:netParams];
    NSLog(@"%@", resultMap);
     */
    

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
